package com.itheima.interceptor;

import com.itheima.common.TokenHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wuzhiyong
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("AuthorizationUser");
        if (token == null) {
            return false;
        }
        String userId = redisTemplate.opsForValue().get(token);
        TokenHolder.setCurrentId(userId);
        return true;
    }

}

